
package rentalgui;

import database.ProviderDao;
import database.ServiceDao;
import java.util.Collection;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import rentaldb.Provider;
import rentaldb.Service;

public class ProviderMainWindow extends javax.swing.JFrame {

    /**
     * Creates new form ExpenseWindow
     */
    public ProviderMainWindow(ServiceDao sdao, ProviderDao provdao) {
        initComponents();
        this.sdao = sdao;
        this.provdao = provdao;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        querySelection = new javax.swing.JComboBox();
        queryButton = new javax.swing.JButton();
        newEntryButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();
        queryContents = new javax.swing.JTextField();
        exitButton = new javax.swing.JButton();
        updateDeleteButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Rental Database - Provider");

        querySelection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Display All", "Find By Provider ID" }));
        querySelection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                querySelectionActionPerformed(evt);
            }
        });

        queryButton.setText("Run Query");
        queryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                queryButtonActionPerformed(evt);
            }
        });

        newEntryButton.setText("New Entry");
        newEntryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newEntryButtonActionPerformed(evt);
            }
        });

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Provider ID", "Name", "Email", "Office Phone", "Mobile Phone", "Fax", "Address", "State", "Zip", "Service"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        resultTable.setFillsViewportHeight(true);
        resultTable.getTableHeader().setResizingAllowed(false);
        resultTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(resultTable);

        queryContents.setEnabled(false);

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        updateDeleteButton.setText("Update/Delete");
        updateDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateDeleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(querySelection, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(queryContents)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(queryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newEntryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(updateDeleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(querySelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(queryContents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(queryButton)
                    .addComponent(newEntryButton)
                    .addComponent(updateDeleteButton)
                    .addComponent(exitButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void querySelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_querySelectionActionPerformed
        switch(querySelection.getSelectedIndex()){
            case 0:
                queryContents.setEnabled(false);
                queryContents.setText("");
                break;
            case 1:
                queryContents.setEnabled(true);
                queryContents.setText("");
                break;
        }
    }//GEN-LAST:event_querySelectionActionPerformed

    private void queryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_queryButtonActionPerformed
        switch(querySelection.getSelectedIndex()){
            case 0:
                selectAll();
                break;
            case 1:
                selectById();
                break;
        }
    }//GEN-LAST:event_queryButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        dispose();
    }//GEN-LAST:event_exitButtonActionPerformed

    private void newEntryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newEntryButtonActionPerformed
        JFrame newWindow = new ProviderInputWindow(sdao, provdao);
        newWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        newWindow.setLocationRelativeTo(this);
        newWindow.setVisible(true);
    }//GEN-LAST:event_newEntryButtonActionPerformed

    private void updateDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateDeleteButtonActionPerformed
        JFrame newWindow = new ProviderUpdateDeleteWindow(sdao, provdao);
        newWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        newWindow.setLocationRelativeTo(this);
        newWindow.setVisible(true);
    }//GEN-LAST:event_updateDeleteButtonActionPerformed

    private void selectAll(){
        DefaultTableModel model = (DefaultTableModel)resultTable.getModel();
        model.setRowCount(0);
        try{
            List<Provider> lst = provdao.getAllProviders();
            for(Provider provider : lst){
                Object[] newRow = new Object[10];
                newRow[0] = provider.getProviderId();
                newRow[1] = provider.getName();
                newRow[2] = provider.getEmail();
                newRow[3] = provider.getOfficephone();
                newRow[4] = provider.getMobilephone();
                newRow[5] = provider.getFax();
                newRow[6] = provider.getAddress();
                newRow[7] = provider.getState();
                newRow[8] = provider.getZip();
                Collection<Service> ser = provider.getServiceCollection();
                String serIds = "";
                for(Service s : ser){
                    serIds += s.getServiceId() + ", ";
                }
                newRow[9] = serIds;
                model.addRow(newRow);
            }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(this, "Error loading items from database.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void selectById(){
        DefaultTableModel model = (DefaultTableModel)resultTable.getModel();
        model.setRowCount(0);
        try{
            if(isInteger(queryContents.getText().trim())){
                Integer curInt = Integer.parseInt(queryContents.getText().trim());
                Provider provider = provdao.getProviderById(curInt);
                Object[] newRow = new Object[10];
                newRow[0] = provider.getProviderId();
                newRow[1] = provider.getName();
                newRow[2] = provider.getEmail();
                newRow[3] = provider.getOfficephone();
                newRow[4] = provider.getMobilephone();
                newRow[5] = provider.getFax();
                newRow[6] = provider.getAddress();
                newRow[7] = provider.getState();
                newRow[8] = provider.getZip();
                Collection<Service> ser = provider.getServiceCollection();
                String serIds = "";
                for(Service s : ser){
                    serIds += s.getServiceId() + ", ";
                }
                newRow[9] = serIds;
                model.addRow(newRow);
            }        
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(this, "Error loading items from database.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private boolean isInteger(String input){
        try{
            Integer.parseInt(input);
            return true;
        }
        catch(Exception e){
            return false;
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton exitButton;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton newEntryButton;
    private javax.swing.JButton queryButton;
    private javax.swing.JTextField queryContents;
    private javax.swing.JComboBox querySelection;
    private javax.swing.JTable resultTable;
    private javax.swing.JButton updateDeleteButton;
    // End of variables declaration//GEN-END:variables
    private ServiceDao sdao; 
    private ProviderDao provdao;
}
